/***************************************************************************
 *   oboe.controllers.NodeController					   *
 *   Copyright (C) 2011 by Felipe Manga					*
 *   lufeboma@gmail.com							*
 *									 *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the	   *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to	*
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:						 *
 *									 *
 *   The above copyright notice and this permission notice shall be	*
 *   included in all copies or substantial portions of the Software.	   *
 *									 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,	   *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF	*
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR	 *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.					   *
 ***************************************************************************/

package oboe.controllers;

import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Sprite;

import oboe.core.Controller;
import oboe.core.Oboe;
import oboe.core.RenderSettings;
import oboe.math.Mesh2D;
import oboe.math.Number3D;

class NodeController extends Controller
{
private var mesh:Mesh2D;
public var target:DisplayObject; // target must be public so SceneController can access it for Z-sorting.
private var renderSettings:RenderSettings;
private var getZSortPosition:Function;

public function new( mesh:Mesh2D, target:DisplayObject=null )
{
	if( !target ) target = new Sprite();
	this.target = Sprite(target);
	super( this.target );
	
	this.renderSettings = new RenderSettings();
	switch( this.renderSettings.projection )
	{
	case RenderSettings.PROJECT_PERSPECTIVE:
		this.getZSortPosition = renderSettings.SORT_PERSPECTIVE;
		break;
	case RenderSettings.PROJECT_PARALLEL:
		this.getZSortPosition = renderSettings.SORT_PARALLEL;
		break;
	case RenderSettings.PROJECT_ISOMETRIC:
		this.getZSortPosition = renderSettings.SORT_ISOMETRIC;
	}
	
	this.mesh = mesh;
	if( target is Sprite )
	{
	this.renderSettings.target = Sprite(target).graphics;
	}
}

private function getMesh():Mesh2D
{
	return this.mesh;
}

public function drawOnTarget( target:Graphics ):Void
{
	var oldTarget:Graphics = this.renderSettings.target;
	this.renderSettings.target = target;
	this.mesh.draw( renderSettings );
	this.renderSettings.target = oldTarget;
}

protected override function setDisplayObject(ndo:DisplayObject, preserveCallbacks:Bool=false):Void
{
	this.target = ndo;
	super.setDisplayObject( ndo, preserveCallbacks );
}

public function callGetZSortPosition():Float
{
	return this.getZSortPosition.apply( this.renderSettings );
}

private function render():Void
{
	renderSettings.target.clear();
	this.mesh.draw( renderSettings );
	target.x = renderSettings.containerShiftOut.x;
	target.y = renderSettings.containerShiftOut.y;
}

public function slot_onGet3DPosition( p:Float3D ):Void
{
	p.copy3( this.renderSettings.position );
}
}